/*
	The MIT License

	Copyright (c) 2008 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

#include "maya_local.h"


/*-----------------------------------------------------------------------------
	Eclipse Entity representation : 
-----------------------------------------------------------------------------*/

EMEntity::EMEntity() {}
EMEntity::~EMEntity() {}
MTypeId EMEntity::id	=	0xEE000001;


//
//	EMEntity::initialize
//
MStatus EMEntity::initialize()
{ 
	return MS::kSuccess;
}


//
//	EMEntity::compute
//	- nothing to compute, so return kUnknownParameter
//
MStatus EMEntity::compute( const MPlug& /*plug*/, MDataBlock& /*data*/ )
{ 
	return MS::kUnknownParameter;
}


//
//	EMEntity::draw
//
void EMEntity::draw( M3dView & view, const MDagPath & path, 
							 M3dView::DisplayStyle style,
							 M3dView::DisplayStatus status )
{ 
	#define LEAD_COLOR				18	// green
	#define ACTIVE_COLOR			15	// white
	#define ACTIVE_AFFECTED_COLOR	8	// purple
	#define DORMANT_COLOR			4	// blue
	#define HILITE_COLOR			17	// pale blue
	

	view.beginGL(); 

	glPushAttrib( GL_CURRENT_BIT );


		if ( ( style == M3dView::kFlatShaded ) || 
			 ( style == M3dView::kGouraudShaded ) ) 
		{  
			//	FLAT AND GOURAUD SHADING :
		}
		
		glLineWidth(3);
		
		MBoundingBox box	= boundingBox();
		MPoint	p1			= box.min();
		MPoint	p2			= box.max();
		float size = 0.5 * (p2.x - p1.x);

		//
		//	Draw arrow, which point local object X, direction of view.	
		//
		M3dView::ColorTable activeColorTable = M3dView::kActiveColors;
		M3dView::ColorTable dormantColorTable = M3dView::kDormantColors;
		
		if ( status==M3dView::kLead )			view.setDrawColor( LEAD_COLOR,		activeColorTable );
		if ( status==M3dView::kActive )			view.setDrawColor( ACTIVE_COLOR,	activeColorTable );
		if ( status==M3dView::kActiveAffected )	view.setDrawColor( ACTIVE_AFFECTED_COLOR, activeColorTable );
		if ( status==M3dView::kDormant )		view.setDrawColor( DORMANT_COLOR,	dormantColorTable );
		if ( status==M3dView::kHilite )			view.setDrawColor( HILITE_COLOR,	activeColorTable );

		glBegin( GL_LINES );
			glVertex3f(p2.x, 0, 0);
			glVertex3f(p2.x + size, 0, 0);

			glVertex3f(p2.x + size, 0, 0);
			glVertex3f(p2.x + size - 0.3 * size, 0,  0.3 * size);

			glVertex3f(p2.x + size, 0, 0);
			glVertex3f(p2.x + size - 0.3 * size, 0, -0.3 * size);
		glEnd();
		
		glBegin( GL_LINES );
			glVertex3f(-0.25,  0.00,  0.00);
			glVertex3f( 0.25,  0.00,  0.00);
			glVertex3f( 0.00, -0.25,  0.00);
			glVertex3f( 0.00,  0.25,  0.00);
			glVertex3f( 0.00,  0.00, -0.25);
			glVertex3f( 0.00,  0.00,  0.25);
		glEnd( );

		glLineWidth(1);

		//
		//	Draw bounding box of the object :
		//
		glBegin( GL_LINES );
			glVertex3f(p1.x, p1.y, p1.z);		glVertex3f(p1.x, p1.y, p2.z);
			glVertex3f(p1.x, p2.y, p1.z);		glVertex3f(p1.x, p2.y, p2.z);
			glVertex3f(p2.x, p1.y, p1.z);		glVertex3f(p2.x, p1.y, p2.z);
			glVertex3f(p2.x, p2.y, p1.z);		glVertex3f(p2.x, p2.y, p2.z);
		glEnd();
		glBegin( GL_LINE_LOOP );
			glVertex3f(p1.x, p1.y, p1.z);
			glVertex3f(p1.x, p2.y, p1.z);
			glVertex3f(p2.x, p2.y, p1.z);
			glVertex3f(p2.x, p1.y, p1.z);
		glEnd();
		glBegin( GL_LINE_LOOP );
			glVertex3f(p1.x, p1.y, p2.z);
			glVertex3f(p1.x, p2.y, p2.z);
			glVertex3f(p2.x, p2.y, p2.z);
			glVertex3f(p2.x, p1.y, p2.z);
		glEnd();
			
		glLineWidth(1);

		//
		//	TEXT stuff :
		//
		MString text;

		MMatrix	V;
		MVector d;		
		view.modelViewMatrix(V);
		d.x  = V.matrix[3][0];		d.y  = V.matrix[3][1];		d.z  = V.matrix[3][2];
		float dist = d.length();

		if (dist<60) {
			RenderText(view, 20, MPoint(0,0,0,1), MString("classname: "),	Maya_ReadString(thisMObject(), "elv_classname") );
			//RenderText(view, 40, MPoint(0,0,0,1), MString("DAG path:"),		path.fullPathName() );
		}
		
		
	glPopAttrib( );

	view.endGL();
}


//
//	EMEntity::RenderText
//
MStatus EMEntity::RenderText(M3dView &view, float offset_y, MPoint origin, MString &left, MString &right)
{
	MMatrix V, P;
	view.modelViewMatrix(V);
	view.projectionMatrix(P);

	float dist;
	MVector d;		
	MVector dy;
	
	//  Get text offset direction :
	dy.x = V.matrix[0][1];		dy.y = V.matrix[1][1];		dy.z = V.matrix[2][1];
	
	//	Get distance :
	d.x  = V.matrix[3][0];		d.y  = V.matrix[3][1];		d.z  = V.matrix[3][2];
	dist = d.length();

	//	Get proj matrix properties :
	float zn = P.matrix[3][2] / P.matrix[2][2];
	float zf = P.matrix[2][2] * zn / (1 + P.matrix[2][2]);
	float w  = 2 * zn / P.matrix[0][0];
	float h  = 2 * zn / P.matrix[1][1];

	unsigned int vx, vy, vw, vh;
	view.viewport(vx, vy, vw, vh);

	//	Compute vertical step :		
	MVector offset = dy;
	offset *= (dist * h * offset_y / vh / zn);

	MPoint p = origin + offset;	
	
	view.drawText(left,		p, M3dView::kRight);
	view.drawText(right,	p, M3dView::kLeft);
	
	return MS::kSuccess;
}


//
//	EMEntity::isBounded
//
bool EMEntity::isBounded() const
{ 
	return true;
}


//
//	EMEntity::boundingBox
//
MBoundingBox EMEntity::boundingBox() const
{   
	EVector	sz	=	Maya_ReadVector(thisMObject(), "editor_bbox", EVector(1,1,1));
	
	MPoint corner1( -sz.x, -sz.y, -sz.z );
	MPoint corner2(  sz.x,  sz.y,  sz.z );
	return MBoundingBox( corner1, corner2 );
}


//
//	EMEntity::creator
//
void* EMEntity::creator()
{
	return new EMEntity();
}


